<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Rest extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->checkAdmin($this->session->userdata('admin_id'));
        $this->data['controller'] = 'rest';         
        $this->load->model('Mrest');
        
    }
    
    public function index()
    {
        $this->data['id_active'] = $this->getParamUri(2,2);
        
        $this->load->library('pagination');
        $config['per_page'] = 30;
        $this->data['page'] = $this->data['pageg'] = $this->data['pageCurrent'] = $this->getParamUri(2,2);
        
        $crew_id            = $this->getParamInt('crew-list');
        $this->data['listcrew']     = $this->Mrest->crewOption($crew_id);
        $this->data['start_date_search']   = $this->getParamString('start-date-search');
        $this->data['end_date_search']   = $this->getParamString('end-date-search');
        
        $data = $this->Mrest->getRestTimeDb($config['per_page'],$this->getParamUri(2,2));
        
        $config['base_url'] = base_url($this->uri->segment(1).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 2;
        $config['uri_segment_page'] = $this->getParamUri(2,2);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
        $this->data['data'] = $data['data'];
        
	    $this->data['action'] = 'index';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function addRestTime()
    {
        $crew_id            = $this->getParamString('crew-list');
        $date_rest          = $this->getParamString('date-rest');
        $start_crest_time   = (float)$this->getParamString('start-crest-time');
        $end_crest_time     = (float)$this->getParamString('end-crest-time');
        $type               = $this->getParamString('ect-crest');
        $note               = $this->getParamString('note-crest-time');
        
        $this->data['select_crew'] = $this->Mrest->crewOption($crew_id);
        $this->data['date_rest'] = $date_rest;
        $this->data['select_time_start'] = $this->Mrest->timeOption($start_crest_time);
        $this->data['select_time_end'] = $this->Mrest->timeOption($end_crest_time);
        $this->data['select_exception'] = $this->Mrest->exceptionOption($type);
        $this->data['note'] = $note;
        
        if ($_SERVER['REQUEST_METHOD'] == 'POST')
        {
            $this->data['message'] = $this->Mrest->validateAddRest($crew_id, $date_rest, $start_crest_time, $end_crest_time, $type);
            
            if($this->data['message'] == '')
            {
                $run = $this->Mrest->addRestTimeDb();
                if($run == 200)
                {
                    die('<meta charset="utf-8"><script>alert("Thêm thời gian nghỉ thành công");window.location.href="'.$this->data['baseurl'].'rest.html";</script>');
                }
            }
            
        }
        
        $this->data['action'] = 'addRestTime';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function editRestTime()
    {
        
        $this->data['id'] = $this->getParamInt('id');
        $this->data['data'] = $this->Mrest->getRestTimeEdit($this->data['id']);
        
        $this->data['crew_id'] = $this->data['data']->crew_id;
        $this->data['date_rest'] = $this->data['data']->rest_day;
        $this->data['type'] = $this->data['data']->type;
        
        $this->data['select_crew'] = $this->Mrest->crewOption($this->data['data']->crew_id);
        $this->data['select_time_start'] = $this->Mrest->timeOption($this->data['data']->rest_time_from);
        $this->data['select_time_end'] = $this->Mrest->timeOption($this->data['data']->rest_time_to);
        $this->data['select_exception'] = $this->Mrest->exceptionOption($this->data['data']->type);
        
        if ($_SERVER['REQUEST_METHOD'] == 'POST')
        {
            $crew_id            = $this->getParamString('crew-id-hd');
            $date_rest          = $this->getParamString('date-rest_hd');
            
            $start_crest_time   = (float)$this->getParamString('start-crest-time');
            $end_crest_time     = (float)$this->getParamString('end-crest-time');
            $type               = $this->getParamString('type-hd');
            $note               = $this->getParamString('note-crest-time');
            
            $this->data['select_crew'] = $this->Mrest->crewOption($crew_id);
            $this->data['date_rest'] = $date_rest;
            $this->data['select_time_start'] = $this->Mrest->timeOption($start_crest_time);
            $this->data['select_time_end'] = $this->Mrest->timeOption($end_crest_time);
            $this->data['select_exception'] = $this->Mrest->exceptionOption($type);
            $this->data['note'] = $note;
            
          
            $this->data['message'] = $this->Mrest->validateEditRest($crew_id, $date_rest, $start_crest_time, $end_crest_time, $type);
            
            if($this->data['message'] == '')
            {
            
                $run = $this->Mrest->editRestTimeDb($this->data['id'], $crew_id, $date_rest, $type);
                if($run == 200)
                {
                    die('<meta charset="utf-8"><script>alert("Sửa thời gian nghỉ thành công");window.location.href="'.$this->data['baseurl'].'rest.html";</script>');
                }
            }
        }
        
        $this->data['action'] = 'editRestTime';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function deleteRestTime()
    {
        $id = $this->getParamInt('id');
        $run = $this->Mrest->deleteRestTimeDB($id);
        if($run == 200)
        {
            die('<meta charset="utf-8"><script>alert("Xóa thời gian nghỉ thành công");window.location.href="'.$this->data['baseurl'].'rest.html";</script>');
        }
        
    }
    
    
}
